import requests
import pymongo
import time

client = pymongo.MongoClient(host="localhost", port=27017)
client.drop_database("3602")
db = client.get_database("3602")
collection = db.get_collection("榜单")

for i in range(1, 7):
    # i = 2
    url = f"https://api.web.360kan.com/v1/rank?cat={i}"
    response = requests.get(url)
    time.sleep(1)
    # print(response)
    datas = response.json()
    # print(datas, type(datas))
    type = {
        1: "电影",
        2: "电视剧",
        3: "综艺",
        4: "动漫",
        5: "动漫",
        6: "儿童"
    }

    l = []

    for data in datas["data"]:
        # print(data)
        d = {
            "名字": data['title'],
            "全集": data['upinfo'],
            "类型": f"{type[data['cat']]}|{data['pubdate'][:4]}",
            "分类": type[data['cat']],
            "热度": data['pv'],
            "简介": data['description'],
            "时间": data['pubdate'],
        }
        l.append(d)
    collection.insert_many(l)
client.close()
